//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.zhiyume.api.controller;

import com.zhiyume.api.util.ApiOperateEnum;
import com.zhiyume.controller.BaseController;
import com.zhiyume.dao.api.TActivityMapper;
import com.zhiyume.dao.entity.TActivity;
import com.zhiyume.dao.entity.TActivityCriteria;
import com.zhiyume.dao.entity.TActivityCriteria.Criteria;
import com.zhiyume.page.PageReq;
import com.zhiyume.page.Pager;
import com.zhiyume.service.TActivityService;
import com.zhiyume.service.common.ConstantEnum;
import com.zhiyume.service.util.DateUtil;
import com.zhiyume.service.util.JsonUtil;
import com.zhiyume.util.TypeConstants;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping({"/api/activity"})
@Api(tags = "活动请求")
public class ActivityAction extends BaseController {
    private static final Logger logger = LoggerFactory.getLogger(ActivityAction.class);
    @Autowired
    private TActivityService activityService;
    @Autowired
    private TActivityMapper tActivityMapper;
    private String successType;
    private String successMsg;

    public ActivityAction() {
        this.successType = ConstantEnum.CONSTANT_SUCCESS.getErrType();
        this.successMsg = ConstantEnum.CONSTANT_SUCCESS.getErrMsg();
    }

    @RequestMapping(value="/list", method={RequestMethod.GET,RequestMethod.POST})
    @ApiOperation(value = "查询活动")
    @ResponseBody
    public Map queryActivity(PageReq page) throws Exception {
        Pager p = this.getDataPager(page);
        return JsonUtil.returnResult(this.successType, this.successMsg, p);
    }

    private Pager getDataPager(PageReq page) throws Exception {
        Pager p = new Pager();
        if (page.getPageSize() > 2000) {
            throw new Exception("pageSize超负荷");
        } else {
            TActivityCriteria t = new TActivityCriteria();
            Criteria tc = t.createCriteria();
            t.setLimitStart(0);
            t.setLimitEnd(page.getPageSize());
            tc.andIsShowNotEqualTo(Integer.valueOf("1"));
            tc.andStatusEqualTo(Integer.valueOf(TypeConstants.activityStatusMap.get("ACTIVITY_PUBLISH").toString()));
            this.orderByAndSetId(page, t, tc);
            p.setTotalCount(this.tActivityMapper.countByExample(t));
            List<TActivity> ts = this.tActivityMapper.selectByExample(t);
            p.setPageSize(page.getPageSize());
            p.setItems(this.getRenderData(ts));
            return p;
        }
    }

    private void orderByAndSetId(PageReq page, TActivityCriteria t, Criteria ct) {
        if (ApiOperateEnum.UP.getName().equals(page.getHandle())) {
            ct.andIdGreaterThan(page.getDataId());
            t.setOrderByClause("publish_time desc, id asc,`a_id` asc");
        } else if (ApiOperateEnum.DOWN.getName().equals(page.getHandle())) {
            ct.andIdLessThan(page.getDataId());
            t.setOrderByClause("publish_time desc, id desc,`a_id` desc");
        } else if (ApiOperateEnum.REFRESH.getName().equals(page.getHandle())) {
            t.setOrderByClause("publish_time desc, id desc,`a_id` desc");
        } else {
            t.setOrderByClause("publish_time desc, id desc,`a_id` desc");
        }

    }

    private List<Map> getRenderData(List<TActivity> ts) {
        List<Map> datas = new ArrayList();
        Iterator var3 = ts.iterator();

        while(var3.hasNext()) {
            TActivity a = (TActivity)var3.next();
            Map dataItem = new HashMap();
            dataItem.put("title", a.getTitle());
            dataItem.put("img_url", a.getCoverUrl());
            long time = DateUtil.getSysDate().getTime();
            Date publishTime = new Date();
            if (a.getPublishTime() != null) {
                publishTime = DateUtil.getDate(a.getPublishTime(), "yyyy-MM-dd HH:mm:ss");
            }

            Date deadlineTime = new Date();
            if (a.getDeadlineTime() != null) {
                deadlineTime = DateUtil.getDate(a.getDeadlineTime(), "yyyy-MM-dd HH:mm:ss");
            }

            dataItem.put("publishTime", publishTime == null ? time : publishTime.getTime());
            dataItem.put("deadlineTime", deadlineTime == null ? time : deadlineTime.getTime());
            dataItem.put("dataId", a.getId());
            dataItem.put("link", a.getId());
            datas.add(dataItem);
        }

        return datas;
    }
}
